package com.sz.controller;

import com.alibaba.fastjson.JSON;
import com.sz.pojo.RevExp;
import com.sz.pojo.User;
import com.sz.service.RabbitmqSender;
import com.sz.service.RevExpSVImpl;
import com.sz.service.UserSVImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;

@Controller
@RequestMapping("/add")
public class InsertController {
    @Resource
    private RevExpSVImpl revExpSV;
    @Resource
    private UserSVImpl userSV;
    @Resource
    private RabbitmqSender sender;

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }

    @RequestMapping("/newRecord")
    public String addNewRecord(RevExp revExp, HttpServletRequest req){
        int i = revExpSV.addRecord(revExp);
        int auth = (Integer)req.getSession().getAttribute("auth");
        if (auth==0) return "redirect:/query/all0";
        else return "redirect:/query/all2";
    }

    @RequestMapping("/user")
    public String addUser(User user, Model model){
        boolean isHaveUsername = userSV.isHaveUsername(user.getUsername());
        if (isHaveUsername){
            model.addAttribute("ishave","用户名重复！");
            return "forward:/login/toRegister";
        }else {
            String newuser = JSON.toJSONString(user);
            sender.sendMsg(newuser);
            return "redirect:/login/toLogin";
        }
    }
}
